package com.zfl;

public class FindUnsortedSubarray {

    public int findUnsortedSubarray(int[] nums) {

        if (nums.length==0||nums.length==1) {
            return 0;
        }

        int right=0;
        int max=nums[0];

        for (int i = 1; i < nums.length; i++) {
            if (nums[i]<max){
                right=i;
            } else {
                max = nums[i];
            }
        }

        int left= nums.length-1;
        int min = nums[nums.length-1];
        for (int i = nums.length - 2; i >= 0; i--) {
            if (nums[i]>min)   {
                left=i;
            } else {
                min = nums[i];
            }
        }

        return right-left+1;
    }
}
